package org.vs.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.vs.annotation.CheckBalance;
import org.vs.annotation.Login;
import org.vs.domain.dto.PublishDTO;
import org.vs.domain.dto.VoteDTO;
import org.vs.domain.dto.VoteInfoDTO;
import org.vs.result.RestResult;
import org.vs.service.TVoteService;

/**
 * 投票信息接口
 */
@RestController
@RequestMapping("/vote")
@AllArgsConstructor
public class VoteController {
    private TVoteService voteService;

    @Login
    @GetMapping("/all")
    public RestResult list(@RequestParam("page") Integer page,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam("title") String title,
                           @RequestHeader("authorization") String authorization) {
        return voteService.all(page, pageSize, title, authorization);
    }

    @Login
    @GetMapping("/info")
    public RestResult info(@RequestParam("id") Long id, @RequestHeader("authorization") String authorization) {
        return voteService.info(id, authorization);
    }

    @Login
    @PostMapping("/save")
    public RestResult save(@RequestBody VoteInfoDTO voteInfoDTO, @RequestHeader("authorization") String authorization) {
        return voteService.save(voteInfoDTO, authorization);
    }

    @Login
    @DeleteMapping("/delete")
    public RestResult delete(@RequestParam("id") Long id) {
        return voteService.delete(id);
    }

    @Login
    @CheckBalance
    @PostMapping("/publish")
    public RestResult publish(@RequestBody PublishDTO publishDTO, @RequestHeader("authorization") String authorization) throws Exception {
        return voteService.publish(publishDTO, authorization);
    }

    @Login
    @CheckBalance
    @PostMapping("/vote")
    public RestResult vote(@RequestBody VoteDTO voteDTO, @RequestHeader("authorization") String authorization) throws Exception {
        return voteService.vote(voteDTO, authorization);
    }

    @Login
    @GetMapping("/result")
    public RestResult result(@RequestParam("id") Long id, @RequestHeader("authorization") String authorization) throws Exception {
        return voteService.result(id, authorization);
    }

    @Login
    @GetMapping("/hash/message")
    public RestResult hashMessage(@RequestHeader("authorization") String authorization) throws Exception {
        return voteService.hashMessage(authorization);
    }
}
